package com.atom.common.delegate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.task.Task;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.atom.common.DelegateAction;
import com.atom.common.DelegateStatus;
import com.atom.common.DelegateType;
import com.atom.common.VariablesConstant;
import com.atom.common.activiti.ActivitiHandler;
import com.atom.common.remote.palace.RemoteRunningTaskDao;

/**
 * Title: DelCoopDelegateHandler
 * Description: 取消协同代理
 * @author WEWE
 * @date 2017年12月5日 上午11:34:23
 * @version 1.0
 */
@Component
@Transactional
public class DelCoopDelegateHandler {

  private static Logger logger = Logger.getLogger(DelCoopDelegateHandler.class);

  @Resource DelegateInfoDao delegateInfoDao;
  @Resource DelegateLogDao delegateLogDao;
  @Resource ActivitiHandler activitiHandler;
  @Resource HistoricDelegateDao historicDelegateDao;
  @Resource RemoteRunningTaskDao remoteRunningTaskDao;

  public void del(DelegateInfo info) {
    logger.info("DelegateInfo:Id:" + info.getId());
    logger.info("DelegateInfo:DelegateStatus:" + info.getDelegateStatus());
    logger.info("DelegateInfo:Owner:" + info.getOwner());
    logger.info("DelegateInfo:Assignee:" + info.getAssignee());
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    switch (info.getDelegateStatus()) {
      case DelegateStatus.ADD_UNDO : {
        // 将状态改为处理中
        info.setDelegateStatus(DelegateStatus.DEL_DOING);
        delegateInfoDao.update(info);
        // 移入历史记录
        HistoricDelegate hi = new HistoricDelegate();
        BeanUtils.copyProperties(info, hi, "id");
        historicDelegateDao.save(hi);
        // 删除运行记录
        delegateInfoDao.delete(info);
        break;
      }
      case DelegateStatus.ADD_DOING : return;
      case DelegateStatus.ADD_DONE : {
        // 将状态改为处理中
        info.setDelegateStatus(DelegateStatus.DEL_DOING);
        delegateInfoDao.update(info);
        // 将之前的操作回滚
        // 被代理人
        String owner = info.getOwner();
        // 代理人
        String assignee = info.getAssignee();
        List<DelegateLog> logList = delegateLogDao.findByDelegateInfoId(info.getId());
        for (DelegateLog log : logList) {
          Task task = activitiHandler.findTaskByTaskId(log.getTaskId());
          if (task != null) {
            if (task.getAssignee() != null && task.getAssignee().equals(assignee)) {
              // 替换任务受理人
              activitiHandler.updateAssignee(task.getId(), owner);
            }
            // 删除任务候选人
            activitiHandler.removeCandidate(task.getId(), assignee);
          }
          // 更新操作日志
          log.setIsRollBack(1);
          delegateLogDao.update(log);
          // 改平台待办表
          Map<String, String> re = new HashMap<String, String>();
          String sysModID = (String) activitiHandler.findVariableByTaskIdByKey(task.getId(), VariablesConstant.SYSTEM_MODULER);
          re.put("ACTIVITYENTITYID", sysModID + "_" + info.getProcessDefinitionKey() + "_" + task.getId());
          re.put("SYSMODID", sysModID);
          re.put("owner", owner);
          re.put("assignee", assignee);
          re.put("delegateType", DelegateType.COOP + "");
          list.add(re);
        }
        // 移入历史记录
        HistoricDelegate hi = new HistoricDelegate();
        BeanUtils.copyProperties(info, hi);
        historicDelegateDao.save(hi);
        // 删除运行记录
        delegateInfoDao.delete(info);
        remoteRunningTaskDao.updateTodoTasksForDelegate(list, DelegateAction.DEL);
        break;
      }
      case DelegateStatus.DEL_DOING : return;
    }

  }

}
